<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\order\WaPurchasedVehiclesController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaOrdersOperateLogVin
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_orders_operate_log_vin';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_orders_operate_log_vin')
            ->select('*')
            ->where($condition)
            ->get();
        if ($list){
            foreach ($list as $k){
                $vin_detail = WaPurchasedVehicles::find(['vin' => $k->vin]);
                $vin_detail = json_decode($vin_detail);
                if ($vin_detail){

                    if ($vin_detail->status == 1) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '待入库';
                    }elseif ($vin_detail->status == 2) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '已入库';
                    }elseif ($vin_detail->status == 4) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '已上上牌';
                    }elseif ($vin_detail->status == 5) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '已开二手票';
                    }elseif ($vin_detail->status == 6) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '已转移待出口';
                    }elseif ($vin_detail->status == 7) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '已注销';
                    }elseif ($vin_detail->status == 8) {
                        $k->status = $vin_detail->status;
                        $k->status_name = '入库失败';
                    }else {
                        $k->status = '';
                        $k->status_name = '';
                    }

                }else{
                    $k->status = '';
                    $k->status_name = '';
                }
                // 1 预售待采购 2 库存待销售 3 部分预售单（两者都有）
                if ($k->product_type == 1){
                    $k->product_type_name = '预售待采购';
                }elseif ($k->product_type == 2){
                    $k->product_type_name = '库存待销售';
                }elseif ($k->product_type == 3){
                    $k->product_type_name = '部分预售单';
                }else{
                    $k->product_type_name = '';
                }
            }
        }
        return json_encode($list);
    }

    public static function created($data){
        $result = Db::table('wa_orders_operate_log_vin')->insertGetId($data);
        return $result;
    }

    public static function updated($data,$where){
        $result = Db::table('wa_orders_operate_log_vin')->where($where)->update($data);
        return $result;
    }

    public static function geted($file,$where){
        $result = Db::table('wa_orders_operate_log_vin')->select($file)->where($where)
            ->get();
        return json_decode($result);
    }

    public static function delete($where){
        $result = Db::table('wa_orders_operate_log_vin')->where($where)->delete();
        return $result;
    }
    public static function find($condition)
    {
        $res = Db::table('wa_orders_operate_log_vin')
            ->where($condition)
            ->first();
        return json_encode($res);
    }

}